/**
 * $Id$ Copyright 2009-2012 Oak Pacific Interactive. All rights reserved.
 */
package com.shuqi.model.manager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.text.TextUtils;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.bookbag.BookBagDao;
import com.shuqi.common.Config;
import com.shuqi.common.utils.BookBagUtil;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.Util;
import com.shuqi.database.dao.impl.BookBagCatalogDao;
import com.shuqi.database.dao.impl.BookMarkInfoDao;
import com.shuqi.database.model.BookMarkInfo;
import com.shuqi.database.model.UserInfo;


/**
 * 
 * 
 * @author <a href="mailto:">yaozhen</a>
 * @version 2014-3-10下午4:14:02
 */
public class BookMarkManager {
    private static BookMarkManager mInstance;


    private BookMarkManager() {

    }

    public static synchronized BookMarkManager getInstance() {
        if (mInstance == null) {
            mInstance = new BookMarkManager();
        }
        return mInstance;
    }

    /**
     * 删除bookmark list
     */
    public void delBookMarks(final List<BookMarkInfo> markInfos, boolean delFile) {
        if (markInfos == null) {
            return;
        }
        List<BookMarkInfo> shuqiList = new ArrayList<BookMarkInfo>();
        List<BookMarkInfo> shenMaList = new ArrayList<BookMarkInfo>();
        List<BookMarkInfo> loaclList = new ArrayList<BookMarkInfo>();
        List<BookMarkInfo> otherList = new ArrayList<BookMarkInfo>();
        for (BookMarkInfo bookMarkInfo : markInfos) {
            int type = bookMarkInfo.getBookType();
            if (type == BookMarkInfo.TYPE_LOCAL_BOOKMARK
                    || type == BookMarkInfo.TYPE_OLD_BAG_BOOKMARK) {
                loaclList.add(bookMarkInfo);
            } else if (type == BookMarkInfo.TYPE_NEW_PAY_BOOKMARK
                    || type == BookMarkInfo.TYPE_OLD_ONLINE_BOOKMARK) {
                shuqiList.add(bookMarkInfo);
            } else if (type == BookMarkInfo.TYPE_NEW_SHENMA_BOOKMARK) {
                shenMaList.add(bookMarkInfo);
            } else {
                otherList.add(bookMarkInfo);
            }
        }
        delLoacalMarks(loaclList, delFile);
        delShenmaMarks(shenMaList, delFile);
        delShuqiBookMarks(shuqiList, delFile);
        delOtherMarks(otherList);
    }

    /**
     * 删除书旗的书签
     * 
     * @param markInfos
     * @param delFile
     */
    private void delShuqiBookMarks(final List<BookMarkInfo> markInfos, boolean delFile) {
        if (markInfos == null || markInfos.size() == 0) {
            return;
        }
        BookMarkInfoDao.getInstance().delShuqiBookMarkList(markInfos, delFile);
        UserInfo userInfo = MyAccountManager.getInstance(ShuqiApplication.getContext());
        // 移除下载
        String uid = userInfo != null ? userInfo.getUserId() : null;
        for (BookMarkInfo bookMarkInfo : markInfos) {
//            FreeBookDownloadManager.getInstance().delDownTask(uid, bookMarkInfo.getBookId());
            Log4an.e("yjd", "删除uid="+uid+",bid="+bookMarkInfo.getBookId());
            DownLoadShuqiBook.getInstace().deleteDownBookTask(uid, bookMarkInfo.getBookId());
            if (delFile) {
                String temp =
                        Config.DEFAULT_SHUQI_CHAPTER_CACHE_PATH + uid + "/"
                                + bookMarkInfo.getBookId();
                deleteFile(temp);
            }
        }
    }

    /***
     * 合并“云同步书签后得到的书签”和“本地比云同步中更新的书签”
     * 
     * @param onLineList 云端最新书签
     * @param localList 本地新操作出来的书签
     * @return
     */
    public Map<String, BookMarkInfo> mergeBookMarkList(List<BookMarkInfo> onLineList,
            List<BookMarkInfo> localNewList) {
        HashMap<String, BookMarkInfo> map = new HashMap<String, BookMarkInfo>();;
        if (onLineList != null && onLineList.size() > 0) {
            for (BookMarkInfo bookMarkInfo : onLineList) {
                if (bookMarkInfo != null) {
                    map.put(bookMarkInfo.getSynKey(), bookMarkInfo);
                }
            }
        }
        if (localNewList != null && localNewList.size() > 0) { // 本地有更新的书签
            for (BookMarkInfo bookMarkInfo : localNewList) {
                if (bookMarkInfo != null) {
                    map.put(bookMarkInfo.getSynKey(), bookMarkInfo);
                }
            }

        }
        return map;
    }

    /**
     * 删除神马的书签
     * 
     * @param markInfos
     * @param delFile
     */
    private void delShenmaMarks(final List<BookMarkInfo> markInfos, boolean delFile) {
        if (markInfos == null || markInfos.size() == 0) {
            return;
        }
        BookMarkInfoDao.getInstance().delShenMaBookMarkList(markInfos, delFile);
        UserInfo userInfo = MyAccountManager.getInstance(ShuqiApplication.getContext());
        String uid = userInfo != null ? userInfo.getUserId() : null;
        if (delFile) {
            for (BookMarkInfo bookMarkInfo : markInfos) {
                String temp =
                        Config.DEFAULT_YISOU_CHAPTER_CACHE_PATH + uid + "/"
                                + bookMarkInfo.getSourceId() + "/" + bookMarkInfo.getBookId();
                deleteFile(temp);
            }
        }
    }

    /**
     * 删除本地书签
     * 
     * @param markInfos
     * @param delFile
     */
    private void delLoacalMarks(final List<BookMarkInfo> markInfos, boolean delFile) {
        if (markInfos == null || markInfos.size() == 0) {
            return;
        }
        BookMarkInfoDao.getInstance().delBookMarkList(markInfos);
        for (BookMarkInfo bookMarkInfo : markInfos) {
            if (delFile) {
                deleteFile(bookMarkInfo.getFilePath());
                //删除书包相关数据
                if(bookMarkInfo!=null && bookMarkInfo.getBookType()==BookMarkInfo.TYPE_OLD_BAG_BOOKMARK && !TextUtils.isEmpty(bookMarkInfo.getBookId())){
                    String bookId = bookMarkInfo.getBookId();
                    BookBagCatalogDao.getInstance().deleteCatalogByBagBookId(bookId);
                    BookBagDao.getInstance().deleteBookBagsByBookId(bookId);
                    BookBagUtil.deletBookBagData(bookId);
                }
            }
        }

    }


    /**
     * 删除其他类型的书签
     * 
     * @param markInfos
     */
    private void delOtherMarks(final List<BookMarkInfo> markInfos) {
        if (markInfos == null || markInfos.size() == 0) {
            return;
        }
        BookMarkInfoDao.getInstance().delBookMarkList(markInfos);
    }

    /**
     * 删除文件或目录
     * @param tmp
     */
    public void deleteFile(String tmp) {
        if (!TextUtils.isEmpty(tmp)) {
            File dir = new File(tmp);
            if (dir.exists()) {
                try {
                    if (dir.isDirectory()) {
                        Util.forceDelDir(dir);
                    } else {
                        dir.delete();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
